﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using ProgNetComponentsDx.Mode;
using BuildHelper.Work.JAVA;
using ProgNetComponentsDx.ChangeDetect;

namespace BuildHelper.Work.Template
{
    public partial class JavaGridControl : DevExpress.XtraEditors.XtraUserControl
    {
        private ChangeDetectorProvider _ChangeDetectorProvider;
        public JavaGridControl()
        {
            InitializeComponent();
        }
        bool lockChange = false;

        public GridControl MainGrid
        {
            get { return gcMain; }
        }
        public GridView MainGridView
        {
            get { return gvMain; }
        }
        public ModeProvider ModeProvider
        {
            get { return modeProvider1; }
        }
        public DataSet DataSource
        {
            get { return gcMain.DataSource as DataSet; }
            set
            {
                gcMain.DataSource = value;
                vGridControl1.DataSource = value;
                vGridControl1.DataMember = "GCM_SETTINGS";
                if (!this.IsDesignMode())
                {
                    try
                    {
                        this.javaDataSet = (JavaDataSet)value;
                    }
                    catch { }
                }
            }
        }
        public string LayoutName
        {
            get { return layoutProvider1.LayoutName; }
            set
            {
                layoutProvider1.BeginInit();
                layoutProvider1.LayoutName = value;
                layoutProvider1.EndInit();
            }
        }
        public Form OwnerForm
        {
            get { return layoutProvider1.OwnerForm; }
            set { layoutProvider1.OwnerForm = value; }
        }
        public bool OpcjaZmieniajRazem
        {
            get { return optionProvider1.GetSettings(ceZmieniajRazem).OptionId != null; }
            set
            {
                if (value)
                {
                    optionProvider1.GetSettings(ceZmieniajRazem).OptionId = Init.InitFormOptionNames.Work_Group.Java.ZmieniajRazem;
                    optionProvider1.GetSettings(ceZmieniajRazem).AutoSave = true;
                }
                else
                    optionProvider1.GetSettings(ceZmieniajRazem).OptionId = null;
            }
        }

        public ChangeDetectorProvider ChangeDetectorProvider
        {
            get { return _ChangeDetectorProvider; }
            set
            {
                _ChangeDetectorProvider = value;
                if (value != null)
                {
                    value.BeginInit();
                    value.GetSettings(gvMain).DetectChanges = true;
                    foreach (var c in gvMain.Columns)
                        value.GetSettings(c).DetectChanges = true;
                    value.EndInit();
                }
            }
    }
        private void gvMain_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            DataRow r = gvMain.GetFocusedDataRow();
            if (r["STM"].AsString() != "GCM")
            {
                if (ceZmieniajRazem.Checked && !lockChange)
                {
                    lockChange = true;
                    if (e.Column == colBuduj)
                    {
                        gvMain.SetRowCellValue(e.RowHandle, colPakuj, e.Value.AsBool());
                    }
                    else if (e.Column == colPakuj)
                    {
                        gvMain.SetRowCellValue(e.RowHandle, colBuduj, e.Value.AsBool());
                    }
                    lockChange = false;
                }
            }
        }
        private void modeProvider1_CustomGetModes(object sender, ProgNetComponentsDx.Mode.CustomModeSettingsEventArgs e)
        {
            bool isGcm = e.Row != null && e.Row["STM"].AsString() == "GCM";
            if ((sender == colPakuj || sender == colPackWS) && isGcm)
            {
                e.IsReadOnly = true;
            }
        }
       
        #region zaznacz/odznacz
        private void btnZaznaczWszystkoBudowanie_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in javaDataSet.BUILD.Rows)
            {
                r["Buduj"] = true;
                if (ceZmieniajRazem.Checked)
                    r["Pakuj"] = true;
            }
        }

        private void btnOdznaczWszystkoBudowanie_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in javaDataSet.BUILD.Rows)
            {
                r["Buduj"] = false;
                if (ceZmieniajRazem.Checked && r["STM"].AsString() != "GCM")
                    r["Pakuj"] = false;
            }
        }

        private void btnZaznaczWszystkoPakowanie_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in javaDataSet.BUILD.Rows)
            {
                r["Pakuj"] = true;
                if (ceZmieniajRazem.Checked)
                    r["Buduj"] = true;
            }
        }

        private void btnOdznaczWszystkoPakowanie_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in javaDataSet.BUILD.Rows)
            {
                if (r["STM"].AsString() != "GCM")
                    r["Pakuj"] = false;
                if (ceZmieniajRazem.Checked)
                    r["Buduj"] = false;
            }
        }
        #endregion

        private void btnCzyscUstawienia_Click(object sender, EventArgs e)
        {
            foreach (var r in this.javaDataSet.BUILD)
            {
                r.Klasy = string.Empty;
            }
        }

        private void rep_Klasy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DataRow r = gvMain.GetFocusedDataRow();
            if (r != null && r["STM"].IsNotNull())
            {
                using (JavaKlasesForm f = new JavaKlasesForm(r["Klasy"].AsString(), r["STM"].AsString()))
                {
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        r["Klasy"] = f.EditValue;
                        gvMain.UpdateCurrentRow();
                    }
                }
            }
        }

        private void vGridControl1_CellValueChanging(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            if (ChangeDetectorProvider != null)
            {
                ChangeDetectorProvider.CheckChanges(e.Row, vGridControl1.GetCellValue(e.Row, e.RecordIndex), e.Value);
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (javaDataSet.GCM_SETTINGS.Rows.Count > 0)
            {
                foreach (DataColumn c in javaDataSet.GCM_SETTINGS.Columns)
                {
                    javaDataSet.GCM_SETTINGS.Rows[0][c] = true;
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (javaDataSet.GCM_SETTINGS.Rows.Count > 0)
            {
                foreach (DataColumn c in javaDataSet.GCM_SETTINGS.Columns)
                {
                    javaDataSet.GCM_SETTINGS.Rows[0][c] = false;
                }
            }
        }

        private void btnZaznaczGenerated_Click(object sender, EventArgs e)
        {
            foreach (var r in this.javaDataSet.BUILD)
            {
                r.GENERATED = true;
            }
        }

        private void btnOdznaczGenerated_Click(object sender, EventArgs e)
        {
            foreach (var r in this.javaDataSet.BUILD)
            {
                r.GENERATED = false;
            }
        }

        private void btnZaznaczJar_Click(object sender, EventArgs e)
        {
            foreach (var r in this.javaDataSet.BUILD)
            {
                if (r.STM != "GCM")
                    r.PackWS = true;
            }
        }

        private void btnOdznaczJar_Click(object sender, EventArgs e)
        {
            foreach (var r in this.javaDataSet.BUILD)
            {
                r.PackWS = false;
            }
        }

        private void bntWczytajPakiety_Click(object sender, EventArgs e)
        {
            List<string> pliki = new List<string>();
            bool res = JavaKlasesForm.GetFielsFromGroup(OwnerForm, null, pliki);
            if (res)
            {
                DataRow[] rows = this.javaDataSet.BUILD.Select();
                List<string> plikiPerSys = null;
                foreach (DataRow r in rows)
                {
                    plikiPerSys = JavaKlasesForm.GetFilesForSystem(pliki, r["STM"].ToString());
                    r["Klasy"] = plikiPerSys.AsString(Constants.NewLineLinux);
                }
            }
        }
    }
}
